package org.ithasu.tools;

public class Console {

	public static boolean DEBUG = isDebugMode();

	public static boolean isDebugMode() {
		return java.lang.management.ManagementFactory.getRuntimeMXBean()
				.getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
	}

	public static void debug(String message) {
		if (DEBUG) {
			message("Debug: " + String.valueOf(message));
		}
	}

	public static void message(String message) {
		System.out.println(message);
	}

	public static void warning(String message) {
		System.err.println("Warning: " + message);
	}

	public static void error(String message) {
		System.err.println("Error: " + message);
	}

	public static void error(Throwable t) {
		if (t != null) {
			error(t.toString());
			t.printStackTrace(System.err);
		}
	}

	// -- Thread ----------------------------------------

	public static void sleep(int millis) {
		long end = System.currentTimeMillis() + millis;
		long toWait = end - System.currentTimeMillis();
		while (toWait >= 0) {
			try {
				Thread.sleep(toWait);
			} catch (Exception e) {
				// Don't care
			}
			toWait = end - System.currentTimeMillis();
		}
	}
}
